{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "file_name='/media/jh/新加卷/2024_12_04/llms4subjects-main/shared-task-datasets/TIBKAT/tib-core-subjects/data/dev/Article/de/3A175995943X.jsonld'\n",
    "with open(file_name,'r') as f:\n",
    "    data=json.loads(f.read())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len(datas)= 24164\n",
      "train_en.json saved\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import json\n",
    "\n",
    "root='/media/jh/新加卷/2024_12_04/llms4subjects-main/shared-task-datasets/TIBKAT/'\n",
    "root='/media/4t/2024_12_04/llms4subjects-main/shared-task-datasets/TIBKAT/'\n",
    "en_de='en'\n",
    "dev='train'\n",
    "datas = []\n",
    "def get_train_data(json_file):\n",
    "    with open(json_file,'r') as f:\n",
    "        data=json.loads(f.read())\n",
    "    for d in data['@graph']:\n",
    "        if 'subject' in d:\n",
    "            tmp=d\n",
    "            break\n",
    "    item={}\n",
    "    item['title']=tmp['title']\n",
    "    item['abstract']=tmp['abstract']\n",
    "    item['dcterms:subject']=tmp['dcterms:subject']\n",
    "    item['subject']=tmp['subject']\n",
    "    datas.append(item)\n",
    "for dirs in os.listdir(root+f'tib-core-subjects/data/{dev}'):\n",
    "    for file in os.listdir(root+f'tib-core-subjects/data/{dev}/{dirs}/{en_de}'):\n",
    "        get_train_data(root+f'tib-core-subjects/data/{dev}/{dirs}/{en_de}/{file}')\n",
    "            \n",
    "print('len(datas)=',len(datas))\n",
    "with open(root+f'{dev}_{en_de}.json','w') as f:\n",
    "    for item in datas:\n",
    "        f.write(json.dumps(item,ensure_ascii=False)+'\\n')\n",
    "print(f'{dev}_{en_de}.json saved')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Code': 'gnd:4003694-7', 'Classification Number': '00', 'Classification Name': 'Unspezifische Allgemeinwörter', 'Name': 'Ausbreitung', 'Alternate Name': [], 'Related Subjects': [], 'Source': 'Du.'}\n",
      "gnd:1163295434 Airfix Marke\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "file_path='/media/4t/2024_12_04/llms4subjects-main/shared-task-datasets/GND/dataset/GND-Subjects-all.json' #GND-Subjects-tib-core.json'\n",
    "with open(file_path,'r') as f:\n",
    "    data=json.loads(f.read())\n",
    "# codes={}\n",
    "# gnds={}\n",
    "# for i in data:\n",
    "#     cn=i['Classification Number']\n",
    "#     if cn not in codes:\n",
    "#         codes[cn]=1\n",
    "#     else:\n",
    "#         codes[cn]+=1\n",
    "#     co=i['Code']\n",
    "#     if co not in gnds:\n",
    "#         gnds[co]=1\n",
    "#     else:\n",
    "#         gnds[co]+=1\n",
    "# print(len(codes.keys()))\n",
    "# print(len(data))\n",
    "# print(len(gnds.keys()))\n",
    "print(data[0])\n",
    "gnd_id2name={}\n",
    "for i in data:\n",
    "    gnd_id2name[i['Code']]=i['Name']\n",
    "    for j in i['Alternate Name']:\n",
    "        gnd_id2name[i['Code']]=gnd_id2name[i['Code']]+','+j\n",
    "print(i['Code'],gnd_id2name[i['Code']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len(datas)= 4025\n",
      "len(datas)= 28189\n",
      "len(datas)= 45927\n",
      "len(datas)= 48882\n",
      "136856\n",
      "136856\n",
      "48882\n",
      "204739\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import pickle\n",
    "import re\n",
    "root='/media/4t/2024_12_04/llms4subjects-main/shared-task-datasets/TIBKAT/'\n",
    "dev='dev'\n",
    "en_de='en'\n",
    "datas = []\n",
    "with open(root+f'{dev}_{en_de}.json','r') as f:\n",
    "    for line in f.readlines():\n",
    "        datas.append(json.loads(line))\n",
    "print('len(datas)=',len(datas))\n",
    "dev='train'\n",
    "en_de='en'\n",
    "with open(root+f'{dev}_{en_de}.json','r') as f:\n",
    "    for line in f.readlines():\n",
    "        datas.append(json.loads(line))\n",
    "print('len(datas)=',len(datas))\n",
    "dev='train'\n",
    "en_de='de'\n",
    "with open(root+f'{dev}_{en_de}.json','r') as f:\n",
    "    for line in f.readlines():\n",
    "        datas.append(json.loads(line))\n",
    "print('len(datas)=',len(datas))\n",
    "dev='dev'\n",
    "en_de='de'\n",
    "with open(root+f'{dev}_{en_de}.json','r') as f:\n",
    "    for line in f.readlines():\n",
    "        datas.append(json.loads(line))\n",
    "print('len(datas)=',len(datas))\n",
    "\n",
    "x_train=[]\n",
    "s1s=[]\n",
    "cnt=0\n",
    "for t in datas:\n",
    "    if type(t['title'])!=str:\n",
    "        t['title']=' '.join(t['title'])\n",
    "    if type(t['abstract'])!=str:\n",
    "        t['abstract']=' '.join(t['abstract'])\n",
    "    s1s.append(t['title']+'.'+t['abstract'])\n",
    "    for gnd in t['dcterms:subject']:\n",
    "        if type(gnd)==dict:\n",
    "            gnd_id=gnd['@id']\n",
    "        else:\n",
    "            continue\n",
    "        cnt+=1\n",
    "        if gnd_id in gnd_id2name:\n",
    "            gnd_val=gnd_id2name[gnd_id]\n",
    "            item={}\n",
    "            item['sentence2']= 'Name is '+gnd_val\n",
    "            item['sentence1']=t['title']+'.'+t['abstract']\n",
    "            item['label']=1\n",
    "            x_train.append(item)\n",
    "print(len(x_train))      \n",
    "print(cnt) \n",
    "print(len(s1s))\n",
    "print(len(gnd_id2name.values()))     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gnd:4066426-0\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "ss=\"{'@id': 'gnd:4066426-0'}\"\n",
    "print(re.findall(r\"'(.*?)'\",ss)[1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py311",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
